/*
 * @lc app=leetcode.cn id=204 lang=javascript
 *
 * [204] 计数质数
 */

// @lc code=start
/**
 * @param {number} n
 * @return {number}
 */
var countPrimes = function (n) {
  const isPrime = new Array(n).fill(1);
  let count = 0;

  for (let i = 2; i < n; i++) {
    if (isPrime[i]) {
      count += 1;

      // j+=i：表示是i的倍数
      for (let j = i * i; j < n; j += i) {
        isPrime[j] = 0;
      }
    }
  }

  return count;
};

// @lc code=end

// @after-stub-for-debug-begin
module.exports = countPrimes;
// @after-stub-for-debug-end
